package com.live.shop.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.live.shop.R;
import com.live.shop.model.KeywordModel;
import com.live.shop.model.MusicModel;
import com.live.shop.model.ProductGroupModel;
import com.live.shop.model.ProductMaterialRecordModel;
import com.live.shop.model.PrologueModel;
import com.live.shop.model.UserModel;
import com.live.shop.scene.server.adapter.GroupAdapter;
import com.live.shop.scene.server.adapter.MaterialAdapter;
import com.live.shop.scene.server.adapter.OnMaterialClickListener;
import com.live.shop.scene.ws.WsMessageEvent;
import com.live.shop.scene.ws.WsServer;
import com.live.shop.scene.ws.WsStatus;
import com.live.shop.sql.KeywordListDatabase;
import com.live.shop.sql.MusicListDatabase;
import com.live.shop.sql.ProductGroupListDatabase;
import com.live.shop.sql.PrologueListDatabase;
import com.live.shop.sql.UserDatabase;
import com.live.shop.tool.GetUtil;
import com.live.shop.view.ToastBottomUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.List;

public class WsWindowService extends Service {
    //主窗口参数
    WindowManager.LayoutParams mainParams;
    //最小化窗口参数
    WindowManager.LayoutParams minParams;

    RequestManager mRequestManager;
    //窗口管理器
    WindowManager windowManager;

    //最小化窗口
    LinearLayout ws_window_min_linearlayout;
    //最大化按钮
    ImageView ws_window_min_imageview;

    //主窗口
    LinearLayout ws_window_main_linearlayout;

    //view是否已添加
    boolean addMainView = true;
    boolean addMinView = true;

    //标题栏
    ImageView ws_window_main_min;
    ImageView ws_window_main_fixed;
    ImageView ws_window_main_connect;
    ImageView ws_window_main_close;
    //评论
    TextView ws_window_main_comment_txt;
    //产品组标题 与 素材
    RecyclerView ws_window_main_groupname_recyclerview;
    RecyclerView ws_window_main_material_recyclerview;
    //素材配置操作：缩放
    TextView ws_window_main_scalerate_value;
    SeekBar ws_window_main_scalerate_seekbar;
    //素材配置操作：素材显隐
    LinearLayout ws_window_main_material_hidestate_linearlayout;
    TextView ws_window_main_material_hidestate_show;
    TextView ws_window_main_material_hidestate_hide;
    //素材配置操作：素材展示时间状态
    LinearLayout ws_window_main_material_showstate_linearlayout;
    TextView ws_window_main_material_showstate_keep;
    TextView ws_window_main_material_showstate_custom;
    TextView ws_window_main_material_showstate_follow;
    //素材配置操作：回复触发自定义时间
    LinearLayout ws_window_main_material_showtime_linearlayout;
    TextView ws_window_main_material_showtime;
    LinearLayout ws_window_main_material_showtime_sub;
    LinearLayout ws_window_main_material_showtime_add;
    SeekBar ws_window_main_material_showtime_seekbar;
    //素材配置操作：素材位置
    ImageView ws_window_main_material_location_direction;
    TextView ws_window_main_material_location_up;
    TextView ws_window_main_material_location_left;
    TextView ws_window_main_material_location_right;
    TextView ws_window_main_material_location_down;
    //总控操作：产品组保存
    TextView ws_window_main_group_save;
    //总控操作：开关摄像头
    TextView ws_window_main_camera_updown;
    //总控操作：切换摄像头
    TextView ws_window_main_camera_change;
    //总控操作：调整摄像组件
    TextView ws_window_main_camera_scale;
    //总控操作：视频音量
    TextView ws_window_main_video_volume;

    //总控操作：回复总控开关
    TextView ws_window_main_answer_status;
    //总控操作：问答回复开关
    TextView ws_window_main_qa_keyword_status;
    //总控操作：素材回复开关
    TextView ws_window_main_material_keyword_status;
    //总控操作：话术开关
    TextView ws_window_main_prologues_status;
    //总控操作：音乐开关
    TextView ws_window_main_music_status;

    //调整摄像组件状态
    boolean resetCameraViewStatus = false;
    //是否在加载状态
    boolean isLoad = false;
    //产品组适配器
    GroupAdapter groupAdapter;
    //素材适配器
    MaterialAdapter materialAdapter;
    List<ProductMaterialRecordModel> materialList;

    //自身服务ID
    int windowStartId;
    //ws链接状态
    boolean isConnected = false;
    //窗口是否固定
    boolean isFixed = false;

    private boolean prologuesStatus = false;//开场白状态
    private boolean answerStatus = false;//回复总控状态
    private boolean qaKeywordStatus = false;//关键词状态
    private boolean musicStatus = false;//音乐状态
    boolean saveStatue = true;//产品组保存状态 true已保存 false未保存
    private boolean materialKeywordStatus = false;//素材回复状态
    private boolean cameraOpenStatus = false; //摄像头状态 true已打开 false已关闭
    private int cameraDirection = 1; //摄像头方向 0前置 1后置(默认)
    boolean videoVolumeStatus = false;//视频音量状态
    private int productGroupIndex = 0;//当前产品组键值
    private int materialIndex = 0;//当前素材键值
    int locationMoveMax = 5;//点击移动位置方向 移动最大间隔

    UserDatabase userDb;
    KeywordListDatabase keyDb;
    PrologueListDatabase proDb;
    MusicListDatabase musicDb;
    ProductGroupListDatabase groupDb;

    UserModel userModel;
    List<ProductGroupModel> groupList;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        windowStartId = startId;
        return super.onStartCommand(intent, flags, startId);
    }
    //广播接收评论
    private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent != null){
                String userName = intent.getStringExtra("userName");
                String userCom = intent.getStringExtra("userCom");
                int type = intent.getIntExtra("type",0);
                ws_window_main_comment_txt.setText(userName+":"+userCom);
                try {
                    JSONObject pearm = new JSONObject();
                    pearm.put("userName",userName);
                    pearm.put("userCom",userCom);
                    pearm.put("type",type);
                    //发送消息
                    SendMessage(WsStatus.commentVlaue,pearm);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    };

    @SuppressLint("UnspecifiedRegisterReceiverFlag")
    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
        keyDb = new KeywordListDatabase(getApplicationContext());
        proDb = new PrologueListDatabase(getApplicationContext());
        musicDb = new MusicListDatabase(getApplicationContext());
        userDb = new UserDatabase(getApplicationContext());
        groupDb = new ProductGroupListDatabase(getApplicationContext());
        userModel = userDb.select();
        groupList = groupDb.selectAllStatusYes();

        mRequestManager = Glide.with(getApplicationContext());

        //加载主窗口和最小化窗口view
        createMinToucher();
        createMainToucher();

        //刷新产品组列表
        updateGroupList();
        //刷新产品组素材列表（已发送消息）
        updateMaterialList();

        //加载点击事件
        initViewClick();

        //注册广播接收器
        IntentFilter intentFilter = new IntentFilter("AccessibilityServiceComment");
        registerReceiver(broadcastReceiver, intentFilter);

        //震动
        GetUtil.getVibrator(getApplicationContext());
    }
    //加载主窗口布局view
    private void createMainToucher() {
        mainParams = new WindowManager.LayoutParams();
        windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mainParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            mainParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        // 设置效果为背景透明.
        mainParams.format = PixelFormat.RGBA_8888;
        // 设置flags,可触焦
        mainParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        //此窗口不会获得关键输入焦点,该窗口不需要与软输入法进行交互,取消可以正常显示，但是系统的返回键用不了
        //params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        // 设置窗口初始停靠位置.
        mainParams.gravity = Gravity.LEFT | Gravity.CENTER;
        mainParams.x = 0;
        mainParams.y = 0;
        // 设置悬浮窗口长宽.根据内容显示
        mainParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mainParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        LayoutInflater inflater = LayoutInflater.from(getApplication());
        // 获取浮动窗口视图所在布局.
        ws_window_main_linearlayout = (LinearLayout) inflater.inflate(R.layout.ws_window_main, null);
        // 添加
        windowManager.addView(ws_window_main_linearlayout, mainParams);
        //view是否已添加
        addMainView = true;

        // 主动计算出当前View的宽高信息.
        ws_window_main_linearlayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

        //最大化布局
        ws_window_main_min = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_min);
        ws_window_main_fixed = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_fixed);
        ws_window_main_connect = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_connect);
        ws_window_main_close = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_close);
        ws_window_main_comment_txt = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_comment_txt);
        ws_window_main_groupname_recyclerview = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_groupname_recyclerview);
        ws_window_main_material_recyclerview = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_recyclerview);

        ws_window_main_scalerate_value = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_scalerate_value);
        ws_window_main_scalerate_seekbar = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_scalerate_seekbar);

        ws_window_main_material_hidestate_linearlayout = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_hidestate_linearlayout);
        ws_window_main_material_hidestate_show = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_hidestate_show);
        ws_window_main_material_hidestate_hide = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_hidestate_hide);

        ws_window_main_material_showstate_linearlayout = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_showstate_linearlayout);
        ws_window_main_material_showstate_keep = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_showstate_keep);
        ws_window_main_material_showstate_custom = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_showstate_custom);
        ws_window_main_material_showstate_follow = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_showstate_follow);

        ws_window_main_material_showtime_linearlayout = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_showtime_linearlayout);
        ws_window_main_material_showtime = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_showtime);
        ws_window_main_material_showtime_sub = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_showtime_sub);
        ws_window_main_material_showtime_add = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_showtime_add);
        ws_window_main_material_showtime_seekbar = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_showtime_seekbar);

        ws_window_main_material_location_direction = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_location_direction);
        ws_window_main_material_location_up = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_location_up);
        ws_window_main_material_location_left = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_location_left);
        ws_window_main_material_location_right = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_location_right);
        ws_window_main_material_location_down = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_location_down);


        ws_window_main_group_save = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_group_save);
        ws_window_main_camera_updown = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_camera_updown);
        ws_window_main_camera_change = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_camera_change);
        ws_window_main_camera_scale = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_camera_scale);
        ws_window_main_video_volume = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_video_volume);

        ws_window_main_answer_status = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_answer_status);
        ws_window_main_qa_keyword_status = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_qa_keyword_status);
        ws_window_main_material_keyword_status = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_material_keyword_status);
        ws_window_main_prologues_status = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_prologues_status);
        ws_window_main_music_status = ws_window_main_linearlayout.findViewById(R.id.ws_window_main_music_status);
        //窗口移动监听
        ws_window_main_linearlayout.setOnTouchListener(new View.OnTouchListener() {
            private int mainX;
            private int mainY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(!isFixed){
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            mainX = (int) event.getRawX();
                            mainY = (int) event.getRawY();
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int nowX = (int) event.getRawX();
                            int nowY = (int) event.getRawY();
                            int movedX = nowX - mainX;
                            int movedY = nowY - mainY;
                            mainX = nowX;
                            mainY = nowY;
                            mainParams.x = mainParams.x + movedX;
                            mainParams.y = mainParams.y + movedY;
                            windowManager.updateViewLayout(ws_window_main_linearlayout, mainParams);
                            break;
                    }
                }
                return false;
            }
        });
    }

    //加载最小化窗口布局view
    private void createMinToucher(){
        minParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            minParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            minParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        minParams.format = PixelFormat.RGBA_8888;
        minParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        minParams.gravity = Gravity.LEFT | Gravity.CENTER;
        minParams.x = 0;
        minParams.y = 0;
        minParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        minParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //view是否已添加
        addMinView = false;

        LayoutInflater inflater = LayoutInflater.from(getApplication());
        // 获取浮动窗口视图所在布局.
        ws_window_min_linearlayout = (LinearLayout) inflater.inflate(R.layout.ws_window_min, null);
        //测量宽高（未指定）
        ws_window_min_linearlayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        //最小化布局
        ws_window_min_imageview = ws_window_min_linearlayout.findViewById(R.id.ws_window_min_imageview);
        //窗口移动监听
        ws_window_min_imageview.setOnTouchListener(new View.OnTouchListener() {
            private int minX;
            private int minY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        minX = (int) event.getRawX();
                        minY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int nowX = (int) event.getRawX();
                        int nowY = (int) event.getRawY();
                        int movedX = nowX - minX;
                        int movedY = nowY - minY;
                        minX = nowX;
                        minY = nowY;
                        minParams.x = minParams.x + movedX;
                        minParams.y = minParams.y + movedY;
                        windowManager.updateViewLayout(ws_window_min_linearlayout, minParams);
                        break;
                }
                return false;
            }
        });
    }
    //加载点击事件
    private void initViewClick(){
        //最大化
        ws_window_min_imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //移除最小化窗口
                if (addMinView) {
                    windowManager.removeView(ws_window_min_linearlayout);
                    addMinView = false;
                }

                //加载主窗口
                windowManager.addView(ws_window_main_linearlayout,mainParams);
                addMainView = true;
            }
        });
        //最小化
        ws_window_main_min.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //移除主窗口
                if (addMainView) {
                    windowManager.removeView(ws_window_main_linearlayout);
                    addMainView = false;
                }
                //加载最小化窗口
                windowManager.addView(ws_window_min_linearlayout,minParams);
                addMinView = true;
            }
        });
        //窗口固定
        ws_window_main_fixed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isFixed = !isFixed;
                if(isFixed) ws_window_main_fixed.setImageResource(R.mipmap.icon_mark_true);
                else ws_window_main_fixed.setImageResource(R.mipmap.icon_mark_white_false);
            }
        });
        //关闭本服务
        ws_window_main_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopSelf(windowStartId);
            }
        });
        //素材配置操作：缩放
        ws_window_main_scalerate_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //progress为当前数值的大小
                if(resetCameraViewStatus){
                    //调整摄像头
                    SendMessage(WsStatus.cameraScaleRate,progress);
                }else{
                    //加载状态【缩放】进度不可更改和发送消息
                    if(!isLoad){
                        ws_window_main_scalerate_value.setText(String.valueOf(progress));
                        //设置缓存的值
                        groupList.get(productGroupIndex).getListRecord().get(materialIndex).setScaleRate(progress);
                        try {
                            JSONObject pearm = new JSONObject();
                            pearm.put("materialIndex",materialIndex);
                            pearm.put("progress",progress);
                            //发送消息
                            SendMessage(WsStatus.mateialScaleRate,pearm);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //在拖动中会调用此方法
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //停止拖动
                //设置保存状态  true已保存 false未保存
                updateSaveStatus(false);
            }
        });
        //素材配置操作：素材显隐
        ws_window_main_material_hidestate_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置显示  int hideState;//隐藏状态 0 显示 1隐藏
                if(groupList.get(productGroupIndex).getListRecord().get(materialIndex).getHideState() == 1){
                    groupList.get(productGroupIndex).getListRecord().get(materialIndex).setHideState(0);
                    ws_window_main_material_hidestate_hide.setTextColor(getColor(R.color.gray));
                    ws_window_main_material_hidestate_show.setTextColor(getColor(R.color.white));
                    //设置素材列表的显隐状态
                    materialAdapter.setMaterialHideState(materialIndex,0);
                    //设置保存状态  true已保存 false未保存
                    updateSaveStatus(false);
                    try {
                        JSONObject pearm = new JSONObject();
                        pearm.put("materialIndex",materialIndex);
                        pearm.put("hidestate",0);
                        //发送消息
                        SendMessage(WsStatus.mateialHideState,pearm);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        });
        ws_window_main_material_hidestate_hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置隐藏 int hideState;//隐藏状态 0 显示 1隐藏
                if(groupList.get(productGroupIndex).getListRecord().get(materialIndex).getHideState() == 0){
                    groupList.get(productGroupIndex).getListRecord().get(materialIndex).setHideState(1);
                    ws_window_main_material_hidestate_hide.setTextColor(getColor(R.color.white));
                    ws_window_main_material_hidestate_show.setTextColor(getColor(R.color.gray));
                    //设置素材列表的显隐状态
                    materialAdapter.setMaterialHideState(materialIndex,1);
                    //设置保存状态  true已保存 false未保存
                    updateSaveStatus(false);
                    try {
                        JSONObject pearm = new JSONObject();
                        pearm.put("materialIndex",materialIndex);
                        pearm.put("hidestate",1);
                        //发送消息
                        SendMessage(WsStatus.mateialHideState,pearm);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        });
        //素材配置操作：素材展示时间状态
        ws_window_main_material_showstate_keep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProductMaterialRecordModel mModel = groupList.get(productGroupIndex).getListRecord().get(materialIndex);
                // int hideState;//隐藏状态 0 显示 1隐藏
                if(mModel.getHideState() == 1){
                    ToastBottomUtils.Setinfo(getApplicationContext(),"请设置素材显隐状态为显示！");
                    return;
                }
                //int materialShowState;//素材展示时间状态[确保隐藏状态为 0显示，否则不生效]  0长时间（将按照设置的展示时间） 1自义定  2跟随关键词回复时间（必须设置关键词）
                groupList.get(productGroupIndex).getListRecord().get(materialIndex).setMaterialShowState(0);
                ws_window_main_material_showstate_keep.setTextColor(getColor(R.color.white));
                ws_window_main_material_showstate_custom.setTextColor(getColor(R.color.gray));
                ws_window_main_material_showstate_follow.setTextColor(getColor(R.color.gray));
                //设置保存状态  true已保存 false未保存
                updateSaveStatus(false);
                try {
                    JSONObject pearm = new JSONObject();
                    pearm.put("materialIndex",materialIndex);
                    pearm.put("showstate",0);
                    //发送消息
                    SendMessage(WsStatus.mateialShowState,pearm);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        ws_window_main_material_showstate_custom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProductMaterialRecordModel mModel = groupList.get(productGroupIndex).getListRecord().get(materialIndex);
                // int hideState;//隐藏状态 0 显示 1隐藏
                if(mModel.getHideState() == 1){
                    ToastBottomUtils.Setinfo(getApplicationContext(),"请设置素材显隐状态为显示！");
                    return;
                }
                //int materialShowState;//素材展示时间状态[确保隐藏状态为 0显示，否则不生效]  0长时间（将按照设置的展示时间） 1自义定  2跟随关键词回复时间（必须设置关键词）
                groupList.get(productGroupIndex).getListRecord().get(materialIndex).setMaterialShowState(1);
                ws_window_main_material_showstate_keep.setTextColor(getColor(R.color.gray));
                ws_window_main_material_showstate_custom.setTextColor(getColor(R.color.white));
                ws_window_main_material_showstate_follow.setTextColor(getColor(R.color.gray));
                //设置保存状态  true已保存 false未保存
                updateSaveStatus(false);
                try {
                    JSONObject pearm = new JSONObject();
                    pearm.put("materialIndex",materialIndex);
                    pearm.put("showstate",1);
                    //发送消息
                    SendMessage(WsStatus.mateialShowState,pearm);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        ws_window_main_material_showstate_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProductMaterialRecordModel mModel = groupList.get(productGroupIndex).getListRecord().get(materialIndex);
                // int hideState;//隐藏状态 0 显示 1隐藏
                if(mModel.getHideState() == 1){
                    ToastBottomUtils.Setinfo(getApplicationContext(),"请设置素材显隐状态为显示！");
                    return;
                }
                //int materialShowState;//素材展示时间状态[确保隐藏状态为 0显示，否则不生效]  0长时间（将按照设置的展示时间） 1自义定  2跟随关键词回复时间（必须设置关键词）
                groupList.get(productGroupIndex).getListRecord().get(materialIndex).setMaterialShowState(2);
                ws_window_main_material_showstate_keep.setTextColor(getColor(R.color.gray));
                ws_window_main_material_showstate_custom.setTextColor(getColor(R.color.gray));
                ws_window_main_material_showstate_follow.setTextColor(getColor(R.color.white));
                //设置保存状态  true已保存 false未保存
                updateSaveStatus(false);
                try {
                    JSONObject pearm = new JSONObject();
                    pearm.put("materialIndex",materialIndex);
                    pearm.put("showstate",2);
                    //发送消息
                    SendMessage(WsStatus.mateialShowState,pearm);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        //素材配置操作：更改自定义时间
        ws_window_main_material_showtime_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int show_time = groupList.get(productGroupIndex).getListRecord().get(materialIndex).getMaterialKeyShowtime();
                if(show_time==0){
                    ToastBottomUtils.Setinfo(getApplicationContext(),"已是最小值");
                    return;
                }
                show_time --;
                ws_window_main_material_showtime.setText(String.valueOf(show_time));
                groupList.get(productGroupIndex).getListRecord().get(materialIndex).setMaterialKeyShowtime(show_time);
                //设置保存状态  true已保存 false未保存
                updateSaveStatus(false);
                try {
                    JSONObject pearm = new JSONObject();
                    pearm.put("materialIndex",materialIndex);
                    pearm.put("showtime",show_time);
                    //发送消息
                    SendMessage(WsStatus.mateialShowTime,pearm);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        ws_window_main_material_showtime_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int show_time = groupList.get(productGroupIndex).getListRecord().get(materialIndex).getMaterialKeyShowtime();
                if(show_time==600){
                    ToastBottomUtils.Setinfo(getApplicationContext(),"已是最大值");
                    return;
                }
                show_time ++;
                ws_window_main_material_showtime.setText(String.valueOf(show_time));
                groupList.get(productGroupIndex).getListRecord().get(materialIndex).setMaterialKeyShowtime(show_time);
                //设置保存状态  true已保存 false未保存
                updateSaveStatus(false);
                try {
                    JSONObject pearm = new JSONObject();
                    pearm.put("materialIndex",materialIndex);
                    pearm.put("showtime",show_time);
                    //发送消息
                    SendMessage(WsStatus.mateialShowTime,pearm);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        ws_window_main_material_showtime_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //progress为当前数值的大小
                //加载状态【更改自定义时间】进度不可更改和发送消息
                if(!isLoad){
                    ws_window_main_material_showtime.setText(String.valueOf(progress));
                    //设置缓存的值
                    groupList.get(productGroupIndex).getListRecord().get(materialIndex).setMaterialKeyShowtime(progress);
                    try {
                        JSONObject pearm = new JSONObject();
                        pearm.put("materialIndex",materialIndex);
                        pearm.put("showtime",progress);
                        //发送消息
                        SendMessage(WsStatus.mateialShowTime,pearm);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //在拖动中会调用此方法
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //停止拖动
                //设置保存状态  true已保存 false未保存
                updateSaveStatus(false);
            }
        });
        //素材配置操作：素材位置
        ws_window_main_material_location_direction.setOnTouchListener(new View.OnTouchListener() {
            private float centerX;
            private float centerY;
            private long mLastTime = -1;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        centerX = event.getX();
                        centerY = event.getY();
                        mLastTime = event.getEventTime();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        // 获取触摸位置的坐标
                        float nowX = event.getX();
                        float nowY = event.getY();
                        long currentTime = event.getEventTime();
                        // 计算触摸位置与摇杆中心的距离
                        int movedX = (int)(nowX - centerX);//小于0向上移动，大于0向下移动
                        int movedY = (int)(nowY - centerY);//大于0向右移动，小于0向左移动

                        float distance = (float) Math.sqrt(movedX * movedX + movedY * movedY);
                        long timeDelta = currentTime - mLastTime;
                        // 防止除以0
                        if (timeDelta == 0) {
                            break;
                        }
                        // 计算速度
                        float velocity = distance / timeDelta;
                        float damping = 1.0f / (1.0f + velocity);

                        // 应用阻尼效果
                        int movedXLast = (int)(movedX * damping);
                        int movedYLast = (int)(movedY * damping);

                        //Log.d(GetUtil.TAG,"movedX前后:["+movedX+"]["+movedXLast+"],movedY前后:["+movedY+"]["+movedYLast+"],timeDelta:"+timeDelta+",damping:"+damping);

                        mLastTime = currentTime;

                        if(resetCameraViewStatus){
                            //调整摄像头
                            try {
                                JSONObject pearm = new JSONObject();
                                pearm.put("x",movedXLast);
                                pearm.put("y",movedYLast);
                                //发送消息
                                SendMessage(WsStatus.cameraLocation,pearm);
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }else{
                            int local_x = groupList.get(productGroupIndex).getListRecord().get(materialIndex).getLocationX();
                            int local_y = groupList.get(productGroupIndex).getListRecord().get(materialIndex).getLocationY();

                            Log.d(GetUtil.TAG,"LocationX:["+(movedXLast+local_x)+"],LocationY:["+(movedYLast+local_y)+"]");

                            groupList.get(productGroupIndex).getListRecord().get(materialIndex).setLocationX(movedXLast+local_x);
                            groupList.get(productGroupIndex).getListRecord().get(materialIndex).setLocationY(movedYLast+local_y);
                            //设置保存状态  true已保存 false未保存
                            updateSaveStatus(false);

                            try {
                                JSONObject pearm = new JSONObject();
                                pearm.put("materialIndex",materialIndex);
                                pearm.put("x",movedXLast);
                                pearm.put("y",movedYLast);
                                //发送消息
                                SendMessage(WsStatus.mateialLocation,pearm);
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }

                        break;
                }
                return true;
            }
        });
        ws_window_main_material_location_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int local_y = groupList.get(productGroupIndex).getListRecord().get(materialIndex).getLocationY();
                groupList.get(productGroupIndex).getListRecord().get(materialIndex).setLocationY(local_y-locationMoveMax);
                //设置保存状态  true已保存 false未保存
                updateSaveStatus(false);

                try {
                    JSONObject pearm = new JSONObject();
                    pearm.put("materialIndex",materialIndex);
                    pearm.put("x",0);
                    pearm.put("y",-locationMoveMax);
                    //发送消息
                    SendMessage(WsStatus.mateialLocation,pearm);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        ws_window_main_material_location_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int local_x = groupList.get(productGroupIndex).getListRecord().get(materialIndex).getLocationX();
                groupList.get(productGroupIndex).getListRecord().get(materialIndex).setLocationX(local_x-locationMoveMax);
                //设置保存状态  true已保存 false未保存
                updateSaveStatus(false);

                try {
                    JSONObject pearm = new JSONObject();
                    pearm.put("materialIndex",materialIndex);
                    pearm.put("x",-locationMoveMax);
                    pearm.put("y",0);
                    //发送消息
                    SendMessage(WsStatus.mateialLocation,pearm);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        ws_window_main_material_location_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int local_x = groupList.get(productGroupIndex).getListRecord().get(materialIndex).getLocationX();
                groupList.get(productGroupIndex).getListRecord().get(materialIndex).setLocationX(local_x+locationMoveMax);
                //设置保存状态  true已保存 false未保存
                updateSaveStatus(false);
                try {
                    JSONObject pearm = new JSONObject();
                    pearm.put("materialIndex",materialIndex);
                    pearm.put("x",locationMoveMax);
                    pearm.put("y",0);
                    //发送消息
                    SendMessage(WsStatus.mateialLocation,pearm);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        ws_window_main_material_location_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int local_y = groupList.get(productGroupIndex).getListRecord().get(materialIndex).getLocationY();
                groupList.get(productGroupIndex).getListRecord().get(materialIndex).setLocationY(local_y+locationMoveMax);
                //设置保存状态  true已保存 false未保存
                updateSaveStatus(false);
                try {
                    JSONObject pearm = new JSONObject();
                    pearm.put("materialIndex",materialIndex);
                    pearm.put("x",0);
                    pearm.put("y",locationMoveMax);
                    //发送消息
                    SendMessage(WsStatus.mateialLocation,pearm);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        //总控操作：产品组保存
        ws_window_main_group_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (ProductGroupModel m:groupList) {
                    groupDb.updateData(m);
                }
                //设置保存状态  true已保存 false未保存
                updateSaveStatus(true);
            }
        });

        //总控操作：开关摄像头
        ws_window_main_camera_updown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cameraOpenStatus = !cameraOpenStatus;
                ws_window_main_camera_updown.setText(cameraOpenStatus?"已打开摄像头":"已关闭摄像头");
                ws_window_main_camera_updown.setTextColor(getColor(cameraOpenStatus?R.color.white:R.color.gray));
                ws_window_main_camera_change.setTextColor(getColor(cameraOpenStatus?R.color.white:R.color.gray));

                //发送默认后置摄像头消息
                SendMessage(WsStatus.cameraUpdown,cameraOpenStatus);
                if(!cameraOpenStatus && resetCameraViewStatus){
                    cameraOpenStatus = false;
                    ws_window_main_camera_scale.setTextColor(getColor(resetCameraViewStatus?R.color.white:R.color.gray));
                    ws_window_main_material_hidestate_linearlayout.setVisibility(View.VISIBLE);
                    ws_window_main_material_showstate_linearlayout.setVisibility(View.VISIBLE);
                    ws_window_main_material_showtime_linearlayout.setVisibility(View.VISIBLE);
                    ws_window_main_material_location_up.setVisibility(View.VISIBLE);
                    ws_window_main_material_location_down.setVisibility(View.VISIBLE);
                    ws_window_main_material_location_left.setVisibility(View.VISIBLE);
                    ws_window_main_material_location_right.setVisibility(View.VISIBLE);
                }
            }
        });
        //总控操作：切换摄像头
        ws_window_main_camera_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!cameraOpenStatus){
                    ToastBottomUtils.Setinfo(getApplicationContext(),"请先打开摄像头");
                    return;
                }
                cameraDirection = cameraDirection==1?0:1;

                //发送切换摄像头消息
                SendMessage(WsStatus.cameraChange,cameraDirection);
            }
        });
        //总控操作：调整摄像组件
        ws_window_main_camera_scale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!cameraOpenStatus){
                    ToastBottomUtils.Setinfo(getApplicationContext(),"请先打开摄像头");
                    return;
                }
                resetCameraViewStatus = !resetCameraViewStatus;
                ws_window_main_camera_scale.setTextColor(getColor(resetCameraViewStatus?R.color.white:R.color.gray));
                if(resetCameraViewStatus){
                    ws_window_main_material_hidestate_linearlayout.setVisibility(View.INVISIBLE);
                    ws_window_main_material_showstate_linearlayout.setVisibility(View.INVISIBLE);
                    ws_window_main_material_showtime_linearlayout.setVisibility(View.INVISIBLE);
                    //上下键隐藏
                    ws_window_main_material_location_up.setVisibility(View.INVISIBLE);
                    ws_window_main_material_location_down.setVisibility(View.INVISIBLE);
                    ws_window_main_material_location_left.setVisibility(View.INVISIBLE);
                    ws_window_main_material_location_right.setVisibility(View.INVISIBLE);
                }else{
                    ws_window_main_material_hidestate_linearlayout.setVisibility(View.VISIBLE);
                    ws_window_main_material_showstate_linearlayout.setVisibility(View.VISIBLE);
                    ws_window_main_material_showtime_linearlayout.setVisibility(View.VISIBLE);
                    ws_window_main_material_location_up.setVisibility(View.VISIBLE);
                    ws_window_main_material_location_down.setVisibility(View.VISIBLE);
                    ws_window_main_material_location_left.setVisibility(View.VISIBLE);
                    ws_window_main_material_location_right.setVisibility(View.VISIBLE);
                }
            }
        });
        //总控操作：视频静音
        ws_window_main_video_volume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                videoVolumeStatus = !videoVolumeStatus;
                ws_window_main_video_volume.setText(videoVolumeStatus?"视频音量开启":"视频音量关闭");
                ws_window_main_video_volume.setTextColor(getColor(videoVolumeStatus?R.color.white:R.color.gray));
                //发送消息
                SendMessage(WsStatus.videoVolume,videoVolumeStatus);
            }
        });

        //总控操作：回复总控开关
        ws_window_main_answer_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                answerStatus = !answerStatus;
                ws_window_main_answer_status.setText(answerStatus?"已开启回复总控":"已关闭回复总控");
                ws_window_main_answer_status.setTextColor(getColor(answerStatus?R.color.white:R.color.gray));
                //发送消息
                SendMessage(WsStatus.answerStatus,answerStatus);
                if(!answerStatus){
                    if(qaKeywordStatus){
                        qaKeywordStatus = false;
                        ws_window_main_qa_keyword_status.setText(qaKeywordStatus?"已开启问答回复":"已关闭问答回复");
                        ws_window_main_qa_keyword_status.setTextColor(getColor(qaKeywordStatus?R.color.white:R.color.gray));
                    }
                    if(materialKeywordStatus){
                        materialKeywordStatus = false;
                        ws_window_main_material_keyword_status.setText(materialKeywordStatus?"已开启素材回复":"已关闭素材回复");
                        ws_window_main_material_keyword_status.setTextColor(getColor(materialKeywordStatus?R.color.white:R.color.gray));
                    }
                }
            }
        });
        //总控操作：问答回复开关
        ws_window_main_qa_keyword_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!answerStatus) ToastBottomUtils.Setinfo(getApplicationContext(),"请先开启回复总控！");
                
                List<KeywordModel> keyList = keyDb.selectAllStatusYes();
                if(keyList.size() == 0){
                    ToastBottomUtils.Setinfo(getApplicationContext(),"未添加关键词！");
                    return;
                }
                qaKeywordStatus = !qaKeywordStatus;
                ws_window_main_qa_keyword_status.setText(qaKeywordStatus?"已开启问答回复":"已关闭问答回复");
                ws_window_main_qa_keyword_status.setTextColor(getColor(qaKeywordStatus?R.color.white:R.color.gray));

                //发送消息
                SendMessage(WsStatus.qaKeywordStatus,qaKeywordStatus);
            }
        });
        //总控操作：素材回复状态
        ws_window_main_material_keyword_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!answerStatus) ToastBottomUtils.Setinfo(getApplicationContext(),"请先开启回复总控！");

                materialKeywordStatus = !materialKeywordStatus;
                ws_window_main_material_keyword_status.setText(materialKeywordStatus?"已开启素材回复":"已关闭素材回复");
                ws_window_main_material_keyword_status.setTextColor(getColor(materialKeywordStatus?R.color.white:R.color.gray));
                //发送消息
                SendMessage(WsStatus.materialKeywordStatus,materialKeywordStatus);
            }
        });
        //总控操作：话术开关
        ws_window_main_prologues_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<PrologueModel> proList = proDb.selectAllStatusYes();
                if(proList.size() == 0){
                    ToastBottomUtils.Setinfo(getApplicationContext(),"未添加开场白！");
                    return;
                }
                prologuesStatus = !prologuesStatus;
                ws_window_main_prologues_status.setText(prologuesStatus?"已开启话术":"已关闭话术");
                ws_window_main_prologues_status.setTextColor(getColor(prologuesStatus?R.color.white:R.color.gray));

                //发送消息
                SendMessage(WsStatus.prologuesStatus,prologuesStatus);
            }
        });
        //总控操作：音乐开关
        ws_window_main_music_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<MusicModel> musicList = musicDb.selectUseNum0();
                if(musicList.size() == 0){
                    ToastBottomUtils.Setinfo(getApplicationContext(),"未添加背景音乐！");
                    return;
                }
                musicStatus = !musicStatus;
                ws_window_main_music_status.setText(musicStatus?"已开启音乐":"已关闭音乐");
                ws_window_main_music_status.setTextColor(getColor(musicStatus?R.color.white:R.color.gray));
                //发送消息
                SendMessage(WsStatus.musicStatus,musicStatus);
            }
        });
    }
    //刷新产品组列表
    private void updateGroupList(){
        groupAdapter = new GroupAdapter(getApplicationContext(), groupList);
        groupAdapter.setOnMaterialClickListener(new OnMaterialClickListener() {
            @Override
            public void onMaterialClick(int position) {
                //获取Spinner控件的适配器
                productGroupIndex = position;
                //重置当前素材键值
                materialIndex = 0;
                //刷新产品组素材列表（已发送消息）
                updateMaterialList();
            }
        });
        //单行 横向
        ws_window_main_groupname_recyclerview.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL));
        ws_window_main_groupname_recyclerview.setAdapter(groupAdapter);
        //切换默认产品组
        groupAdapter.setGroup(productGroupIndex);
        groupAdapter.notifyDataSetChanged();
    }
    //刷新产品组素材列表(切换产品组)
    private void updateMaterialList(){
        materialList = groupList.get(productGroupIndex).getListRecord();
        materialAdapter = new MaterialAdapter(getApplicationContext(), materialList,mRequestManager);
        //选择素材
        materialAdapter.setOnMaterialClickListener(new OnMaterialClickListener() {
            @Override
            public void onMaterialClick(int position) {
                //素材索引
                materialIndex = position;
                //刷新当前素材的值（已发送消息）
                updateMaterialValue();
            }
        });
        //单行 横向
        ws_window_main_material_recyclerview.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL));
        ws_window_main_material_recyclerview.setAdapter(materialAdapter);
        materialAdapter.notifyDataSetChanged();

        //发送消息
        SendMessage(WsStatus.groupUpdate,productGroupIndex);

        //刷新当前素材的值（已发送消息）
        updateMaterialValue();
    }
    //刷新当前素材的值
    private void updateMaterialValue(){
        ProductMaterialRecordModel nowModel = groupList.get(productGroupIndex).getListRecord().get(materialIndex);
        //设置加载状态为true
        isLoad = true;
        //素材配置操作：缩放
        ws_window_main_scalerate_value.setText(String.valueOf(nowModel.getScaleRate()));
        ws_window_main_scalerate_seekbar.setProgress(nowModel.getScaleRate());

        //素材配置操作：素材显隐 隐藏状态 0 显示 1隐藏
        ws_window_main_material_hidestate_show.setTextColor(getColor(nowModel.getHideState() == 0?R.color.white:R.color.gray));
        ws_window_main_material_hidestate_hide.setTextColor(getColor(nowModel.getHideState() == 1?R.color.white:R.color.gray));
        //素材配置操作：素材展示时间状态 0长时间 1自义定  2跟随关键词回复时间
        ws_window_main_material_showstate_keep.setTextColor(getColor(nowModel.getMaterialShowState() == 0?R.color.white:R.color.gray));
        ws_window_main_material_showstate_custom.setTextColor(getColor(nowModel.getMaterialShowState() == 1?R.color.white:R.color.gray));
        ws_window_main_material_showstate_follow.setTextColor(getColor(nowModel.getMaterialShowState() == 2?R.color.white:R.color.gray));

        //素材配置操作：回复触发自定义时间
        ws_window_main_material_showtime.setText(String.valueOf(nowModel.getMaterialKeyShowtime()));
        ws_window_main_material_showtime_seekbar.setProgress(nowModel.getMaterialKeyShowtime());

        //列表设置选中的素材
        materialAdapter.setLastOrNextMaterial(materialIndex);
        //设置加载状态为false
        isLoad = false;
        //发送消息
        SendMessage(WsStatus.mateialUpdate,materialIndex);
    }

    //设置保存状态  true已保存 false未保存
    private void updateSaveStatus(boolean status){
        saveStatue = status;
        ws_window_main_group_save.setTextColor(getColor(status?R.color.gray:R.color.white));
    }
    //消息发送模版
    public void SendMessage(String magType, Object obj){
        try {
            JSONObject json = new JSONObject();
            json.put("command",magType);
            json.put("param",obj);
            WsServer.SendMsg(json.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //=======================勿动区域=================================
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBusa(WsMessageEvent wsMessageEvent){
        switch (wsMessageEvent.getNumber()) {
            case WsStatus.onOpen://onOpen
                isConnected = WsServer.collections.size()>0;
                ws_window_main_connect.setImageResource(isConnected?R.mipmap.icon_ws_main_connect_yes:R.mipmap.icon_ws_main_connect_no);
                break;
            case WsStatus.onMessageString://onMessage

                break;
            case WsStatus.onClose://onMessage
                isConnected = WsServer.collections.size()>0;
                ws_window_main_connect.setImageResource(isConnected?R.mipmap.icon_ws_main_connect_yes:R.mipmap.icon_ws_main_connect_no);
                break;
            case WsStatus.onError://onError
                ToastBottomUtils.Setinfo(getApplicationContext(),wsMessageEvent.getMessage());
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //移除主窗口
        if (addMainView) {
            windowManager.removeView(ws_window_main_linearlayout);
        }
        if (addMinView) {
            windowManager.removeView(ws_window_min_linearlayout);
        }
        EventBus.getDefault().unregister(this);
        //取消注册广播接收器
        unregisterReceiver(broadcastReceiver);
        //震动
        GetUtil.getVibrator(getApplicationContext());
    }
}